package zucc.edu.cn.chengchengparttime;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import android.app.Fragment;

import zucc.edu.cn.chengchengparttime.Home.HomeFragment;
import zucc.edu.cn.chengchengparttime.My.MyFragment;
import zucc.edu.cn.chengchengparttime.Mysignup.MysignupFragment;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    //定义三个Fragment
    private Fragment homeFragment=new HomeFragment();
    private Fragment mysignupFragment=new MysignupFragment();
    private Fragment myFragment=new MyFragment();

    private FragmentManager fragmentManager; //Fragment管理
    private LinearLayout linearLayout_home,linearLayout_mysignup,linearLayout_my; //三个底部导航栏的控件布局
    private ImageView imageView,imageView2,imageView3; //底部导航栏的图标
    private TextView textView,textView2,textView3; //底部导航栏的文字

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        //找到三个底部导航栏的控件
        linearLayout_home=findViewById(R.id.linearlayout_home);
        linearLayout_mysignup=findViewById(R.id.linearlayout_mysignup);
        linearLayout_my=findViewById(R.id.linearlayout_my);

        //设置监听
        linearLayout_home.setOnClickListener(this);
        linearLayout_mysignup.setOnClickListener(this);
        linearLayout_my.setOnClickListener(this);

        //找到三个底部导航栏的控件的图标
        imageView=findViewById(R.id.imageView);
        imageView2=findViewById(R.id.imageView2);
        imageView3=findViewById(R.id.imageView3);

        //找到三个底部导航栏的控件的文字
        textView=findViewById(R.id.textView);
        textView2=findViewById(R.id.textView2);
        textView3=findViewById(R.id.textView3);

        initFragment(); //初始化Fragment
    }

    private void initFragment(){
        fragmentManager=getFragmentManager();
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        transaction.add(R.id.id_content,homeFragment); //加入到transaction中
        transaction.add(R.id.id_content,mysignupFragment); //加入到transaction中
        transaction.add(R.id.id_content,myFragment); //加入到transaction中
        showcolor(1); //显示点击导航栏图标时的颜色
        showfragment(1); //显示fragment
        transaction.commit(); //提交transaction
    }

    //显示点击导航栏图标时的颜色
    private void showcolor(int i){
        imageView.setImageResource(R.drawable.home);
        textView.setTextColor(Color.BLACK);
        imageView2.setImageResource(R.drawable.mysignup);
        textView2.setTextColor(Color.BLACK);
        imageView3.setImageResource(R.drawable.my);
        textView3.setTextColor(Color.BLACK);
        switch (i){
            case 1:
                imageView.setImageResource(R.drawable.home);
                textView.setTextColor(Color.BLUE);
                break;
            case 2:
                imageView2.setImageResource(R.drawable.mysignup);
                textView2.setTextColor(Color.BLUE);
                break;
            case  3:
                imageView3.setImageResource(R.drawable.my);
                textView3.setTextColor(Color.BLUE);
                break;
            default:
                break;
        }
    }

    //显示fragment
    private void showfragment(int i) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragment(transaction); //隐藏fragment
        switch (i){
            case 1:
                transaction.show(homeFragment);
                break;
            case 2:
                transaction.show(mysignupFragment);
                break;
            case 3:
                transaction.show(myFragment);
                break;
            default:
                break;
        }
        transaction.commit();
    }

    //隐藏fragment
    private void hideFragment(FragmentTransaction transaction) {
        transaction.hide(homeFragment);
        transaction.hide(mysignupFragment);
        transaction.hide(myFragment);
    }

    @Override
    public void onClick(View v){
        switch (v.getId()){
            case R.id.linearlayout_home:
                showfragment(1);
                showcolor(1);
                break;
            case R.id.linearlayout_mysignup:
                showfragment(2);
                showcolor(2);
                break;
            case R.id.linearlayout_my:
                showfragment(3);
                showcolor(3);
                break;
            default:
                break;
        }
    }

    //重写onResume方法，activity跳转到响应的fragment
    @Override
    protected void onResume() {
        int id = getIntent().getIntExtra("id", 0);
        if (id == 1) {
            Fragment fragment = new MyFragment();
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.replace(R.id.linearlayout_home, fragment);
            showcolor(1); //显示点击导航栏图标时的颜色
            showfragment(1); //显示fragment
            transaction.commit();
        }
        if (id == 2) {
            Fragment fragment = new MyFragment();
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.replace(R.id.linearlayout_mysignup, fragment);
            showcolor(2); //显示点击导航栏图标时的颜色
            showfragment(2); //显示fragment
            transaction.commit();
        }
        if (id == 3) {
            Fragment fragment = new MyFragment();
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.replace(R.id.linearlayout_my, fragment);
            showcolor(3); //显示点击导航栏图标时的颜色
            showfragment(3); //显示fragment
            transaction.commit();
        }
        super.onResume();
    }
}
